package org.jivesoftware.smackx;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Mode;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.LastActivity;

public class LastActivityManager
{
  private long lastMessageSent;
  private Connection connection;

  static
  {
    Connection.addConnectionCreationListener(new ConnectionCreationListener()
    {
      public void connectionCreated(Connection paramAnonymousConnection)
      {
        new LastActivityManager(paramAnonymousConnection, null);
      }
    });
  }

  private LastActivityManager(Connection paramConnection)
  {
    this.connection = paramConnection;
    paramConnection.addPacketSendingListener(new PacketListener()
    {
      public void processPacket(Packet paramAnonymousPacket)
      {
        Presence localPresence = (Presence)paramAnonymousPacket;
        Presence.Mode localMode = localPresence.getMode();
        if (localMode == null)
          return;
        switch (localMode)
        {
        case available:
        case away:
          LastActivityManager.this.resetIdleTime();
        }
      }
    }
    , new PacketTypeFilter(Presence.class));
    paramConnection.addPacketListener(new PacketListener()
    {
      public void processPacket(Packet paramAnonymousPacket)
      {
        Message localMessage = (Message)paramAnonymousPacket;
        if (localMessage.getType() == Message.Type.error)
          return;
        LastActivityManager.this.resetIdleTime();
      }
    }
    , new PacketTypeFilter(Message.class));
    paramConnection.addPacketListener(new PacketListener()
    {
      public void processPacket(Packet paramAnonymousPacket)
      {
        LastActivity localLastActivity = new LastActivity();
        localLastActivity.setType(IQ.Type.RESULT);
        localLastActivity.setTo(paramAnonymousPacket.getFrom());
        localLastActivity.setFrom(paramAnonymousPacket.getTo());
        localLastActivity.setPacketID(paramAnonymousPacket.getPacketID());
        localLastActivity.setLastActivity(LastActivityManager.this.getIdleTime());
        LastActivityManager.this.connection.sendPacket(localLastActivity);
      }
    }
    , new AndFilter(new PacketFilter[] { new IQTypeFilter(IQ.Type.GET), new PacketTypeFilter(LastActivity.class) }));
    ServiceDiscoveryManager.getInstanceFor(paramConnection).addFeature("jabber:iq:last");
    resetIdleTime();
  }

  private void resetIdleTime()
  {
    long l = System.currentTimeMillis();
    synchronized (this)
    {
      this.lastMessageSent = l;
    }
  }

  private long getIdleTime()
  {
    long l2 = System.currentTimeMillis();
    long l1;
    synchronized (this)
    {
      l1 = this.lastMessageSent;
    }
    return (l2 - l1) / 1000L;
  }

  public static LastActivity getLastActivity(Connection paramConnection, String paramString)
    throws XMPPException
  {
    LastActivity localLastActivity1 = new LastActivity();
    localLastActivity1.setTo(paramString);
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(new PacketIDFilter(localLastActivity1.getPacketID()));
    paramConnection.sendPacket(localLastActivity1);
    LastActivity localLastActivity2 = (LastActivity)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localLastActivity2 == null)
      throw new XMPPException("No response from server on status set.");
    if (localLastActivity2.getError() != null)
      throw new XMPPException(localLastActivity2.getError());
    return localLastActivity2;
  }

  public static boolean isLastActivitySupported(Connection paramConnection, String paramString)
  {
    try
    {
      DiscoverInfo localDiscoverInfo = ServiceDiscoveryManager.getInstanceFor(paramConnection).discoverInfo(paramString);
      return localDiscoverInfo.containsFeature("jabber:iq:last");
    }
    catch (XMPPException localXMPPException)
    {
    }
    return false;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.LastActivityManager
 * JD-Core Version:    0.6.1
 */